// pages/information/information.ts
var app=getApp();
var accountData=getApp().globalData.accountData;
var apiUrl=getApp().globalData.apiUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{
      loginState:false,
      avatarUrl:"",
      userName:"",
      permission:0,
    },
    role:[],
    oldPass:"",
    newPass:"",
    newPassAgain:"",
  },
  onChooseAvatar(e) {
    console.log(e)
    this.setData({
      userInfo:{
        loginState:this.data.userInfo.loginState,
        avatarUrl:e.detail.avatarUrl,
        userName:this.data.userInfo.userName,
        permission:this.data.userInfo.permission
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
  },

  finishName:function(e){
    if(e.detail.value!=""){
      this.setData({
        userInfo:{
          loginState:this.data.userInfo.loginState,
          avatarUrl:this.data.userInfo.avatarUrl,
          userName:e.detail.value,
          permission:this.data.userInfo.permission
        }
      })
    }
  },
  //保存个人资料
  saveClick(){
    wx.request({
      url: apiUrl+'/user/userInfoModify',
      method: 'POST',
      data: {
        "token":wx.getStorageSync('token'),
        "nickname":this.data.userInfo.userName,
        "avatar_url":this.data.userInfo.avatarUrl
      },
      success:res=>{
        // console.log(res)
        if(res.data.code==20000){
          app.globalData.userInfo=this.data.userInfo
          wx.showToast({
            title: '保存成功',
          })
        }
        setTimeout(
          function(){
            wx.navigateBack()
          }
          ,1000)
        
        
      }
    })
  },
  //修改密码
  finishOldPass(e){
    this.setData({
      oldPass:e.detail.value
    })
  },
  finishNewPass(e){
    this.setData({
      newPass:e.detail.value
    })
  },
  finishNewPassAgain(e){
    this.setData({
      newPassAgain:e.detail.value
    })
  },
  changePass(){
    if(this.data.newPass==="" || this.data.newPassAgain==="" || this.data.oldPass===""){
      wx.showModal({
        title: '温馨提示',
        content: '请输入原密码和新密码',
        showCancel:false
      })
    }
    else if((this.data.oldPass.length==this.data.newPass.length)&& this.data.oldPass.indexOf(this.data.newPass)!=-1){
      wx.showModal({
        title: '温馨提示',
        content: '原密码与新密码相同，请重新输入……',
        showCancel:false
      })
    }
    else if(this.data.newPass===this.data.newPassAgain){
      wx.request({
        url: app.globalData.apiUrl+'/user/ChangePassword',
        method:"POST",
        data:{
          "token":wx.getStorageSync('token'),
          "oldPassword":this.data.oldPass,
          "newPassword":this.data.newPass
        },
        success:(res)=>{
          if(res.data.code==20000){
            wx.showToast({
              title: '修改成功！',
            })
            this.setData({
              oldPass:"",
              newPass:"",
              newPassAgain:""
            })
          }
          else if(res.data.code==20001){
            wx.showModal({
              title: '温馨提醒',
              content: '修改失败,原密码输入有误',
              showCancel:false
            })
          }
          else{
            wx.showModal({
              title: '温馨提醒',
              content: '修改失败',
              showCancel:false
            })
          }
          this.setData({
            oldPass:"",
            newPass:"",
            newPassAgain:""
          })
        }
      })
    }
    else if(this.data.newPass!=this.data.newPassAgain){
      wx.showModal({
        title: '温馨提示',
        content: '两次新密码输入不一致',
        showCancel:false
      })
    }
    else{
      wx.showModal({
        title: '温馨提示',
        content: '修改出错，请检查输入内容是否有误',
        showCancel:false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo:app.globalData.userInfo,
      role:app.globalData.role
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})